package com.changgou.file.util.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author lqq
 * @date 2020-02-24 20:59
 */
@RestController
@RequestMapping("/file")
public class QiniuController {


    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file){

        //获取文件名
        String originalFilename = file.getOriginalFilename();//原始文件名
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);
        String fileName = UUID.randomUUID().toString() + extention;
        try {
            //将文件上传到七牛云服务器
            QiniuUtils.upload2Qiniu(file.getBytes(),fileName);
            //存到redis的一个set集合

        } catch (IOException e) {
            e.printStackTrace();

        }
        //图片上传成功
        return new Result(true, StatusCode.OK,"文件上传成功");
    }

}
